package com.xieli.invitesdk.ui
import android.provider.MediaStore
import android.app.Dialog
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import com.xieli.invitesdk.R
import com.xieli.invitesdk.databinding.QrcedeInvitationDialogLayoutBinding
/**
 * @Auther sr
 * @Date 2023/9
 * @Description 二维码分享弹窗
 */
class QRCodeInvitationDialog(val image:Bitmap,val click:(isLift:Boolean,img:Bitmap?)->Unit) : DialogFragment()  {

    private lateinit var databinding: QrcedeInvitationDialogLayoutBinding

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        // 设置点击空白区域不关闭对话框
        dialog.setCanceledOnTouchOutside(false)
        // 设置对话框样式，使其位于底部
        dialog.window?.apply {
            setBackgroundDrawableResource(android.R.color.transparent)
        }
        return dialog
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            //进出动画
            attributes?.windowAnimations = R.style.QRCodeDialogAnimation
            //dialig的宽度占满
            setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        databinding = DataBindingUtil.inflate(inflater, R.layout.qrcede_invitation_dialog_layout, container, false)

        //设置传入的二维码图片
        databinding.ivQrcode.setImageBitmap(image)

        //弹窗关闭
        databinding.btnClose.setOnClickListener{
            dismiss()
        }
        //保存到相册
        databinding.btnSaveToAlbum.setOnClickListener{
            saveViewToGallery(databinding.clQrcodeBg)
            click.invoke(true,null)
            dismiss()
        }

        //截取图片返回,以分享给好友
        databinding.btnShareWithFriends.setOnClickListener{
            click.invoke(false,captureView(databinding.clQrcodeBg))
            dismiss()
        }

        return databinding.root
    }

    /**
     * 将控件截取成图片并保存到相册
     * @param view 需要截取的控件
     */
    private fun saveViewToGallery(view: View) {
        //截取
        val bitmap = captureView(view)
        val contentResolver = requireContext().contentResolver
        // 保存图片到相册
        MediaStore.Images.Media.insertImage(
            contentResolver,
            bitmap,
            "screenshot",
            "screenshot"
        )

        // 发送广播通知相册有新图片被添加
        val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
        val uri = Uri.parse("file://" + Environment.getExternalStorageDirectory())
        intent.data = uri
        requireContext().sendBroadcast(intent)
        Toast.makeText(context, "已保存", Toast.LENGTH_SHORT).show()
    }
    /**
     * 将控件截取成图片
     * @param view 需要截取的控件
     */
    private fun captureView(view: View): Bitmap {
        // 创建一个空白的 Bitmap 对象
        val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
        // 使用 Canvas 将控件绘制到 Bitmap 上
        val canvas = Canvas(bitmap)
        canvas.drawColor(Color.WHITE) // 设置画布颜色为白色
        view.draw(canvas)
        return bitmap
    }
}